<?php
namespace CechargeChannel;

class Jisuapi extends ChannelAbstract
{
    protected $key    = '';
    protected $secret = '';
    protected $data   = [];
    public function commit($data)
    {
        $config       = json_decode($data['configjson'], true);
        $this->key    = $config['appkey'];
        $this->secret = $config['appsecret'];
        $this->data   = $data;
        return $this->phoneBill();

    }

    // 话费充值
    protected function phoneBill()
    {
        $data = [
            'mobile'     => $this->data['mobile'],
            'amount'     => $this->data['recharge_amount'],
            'outorderno' => $this->data['order_number'],
        ];

        $data['sign'] = $this->makeSign($data, $this->secret);
        $url          = $this->data['domain_name'] . '/mobilerecharge/recharge?appkey=' . $this->key . '&' . http_build_query($data);
        $result       = file_get_contents($url);
        $jsonarr      = json_decode($result, true);

        if ($result == false || $jsonarr['status'] != 0 && $jsonarr['code'] != 212) {
            return [
                'success'          => false,
                'cecharge_channel' => 'Jisuapi',
                'order_number'     => $this->data['order_number'],
                'value'            => json_encode($jsonarr),
            ];
        }
        return [
            'success'          => true,
            'cecharge_channel' => 'Jisuapi',
            'order_number'     => $this->data['order_number'],
            'value'            => json_encode($jsonarr),
        ];

    }

    public function makeSign($queryarr, $appsecret)
    {
        ksort($queryarr, SORT_STRING);
        $str = implode($queryarr);
        $str .= $appsecret;
        //exit($str);
        $str = md5($str);

        return $str;
    }
}
